package com.tenebrous.jestic.compents.config;

import com.tenebrous.jestic.compents.interceptor.SecurityInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Arrays;

@Slf4j
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${spring.context.webconfig.enable:true}")
    private Boolean enable;

    @Value("${spring.context.webconfig.exclude-path}")
    private String excludePath;

    @Value("${spring.context.webconfig.include-path:/**}")
    private String includePath;

    @Autowired
    private SecurityInterceptor securityInterceptor;

    /**
     * 路由拦截设置
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (this.enable) {
            log.debug("开启：自定义路由拦截器......");
            InterceptorRegistration registration = registry.addInterceptor(this.securityInterceptor).addPathPatterns(Arrays.asList(this.includePath.split(",")));
            if (this.excludePath != null && this.excludePath != "") {
                registration.excludePathPatterns(Arrays.asList(this.excludePath.split(",")));
            }
        }
    }

}
